package com.sijobe.installer;

import java.io.File;
import java.lang.reflect.Method;
import java.util.List;

/**
 * This class handles the backup Minecraft worlds event. 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author simo_415
 */
public class ThreadBackupWorlds extends Thread {

   private File mcdir;
   private Method callback;
   
   public ThreadBackupWorlds(File mcdir) {
      this.mcdir = mcdir;
   }

   /**
    * Set the callback method if you want a method called when task has completed
    * 
    * @param callback - The static method to callback
    */
   public void setCallback(Method callback) {
      this.callback = callback;
   }

   /**
    * Signals the specified callback method when the backup is complete
    * 
    * @param err - The message to send the callback method
    */
   private void signalDone(String err) {
      if (callback != null) {
         try {
            callback.invoke(null, new Object[]{err});
         } catch (Exception e) {}
      }
   }
   
   public static String backupWorlds(File mcdir) {
      if (mcdir == null || !mcdir.exists()) {
         return "Couldn't discover Minecraft home directory.";
      }
      File saves = new File(mcdir,"saves");
      if (!saves.exists()) {
         return "Couldn't find Minecraft.jar";
      }
      File destination = new File(mcdir,"backup/saves");
      try {
         destination.mkdirs();
      } catch (Exception e) { }
      if (!destination.exists()) {
         return "Couldn't create destination directory for backup.";
      }
      List in = Util.getFiles(new File(mcdir,"saves"));
      for (int i = 0; i < in.size(); i++) {
         File f = (File)in.get(i);
         if (f.isDirectory()) {
            File dir = new File(destination,f.getName());
            if (!dir.exists()) {
               dir.mkdirs();
            }
            Util.compressFiles(f,new File(dir,f.getName() + "." + System.currentTimeMillis() + ".zip"));
            //Util.compressFiles(f,new File(destination,f.getName() + "/" + f.getName() + "." + System.currentTimeMillis() + ".zip"));
         } else {
            Util.compressFiles(f, new File(destination,f.getName() + "." + System.currentTimeMillis() + ".zip"));
         }
      }
      return null;
   }

   /**
    * Creates a time stamped backup of all your currently worlds
    * 
    * @see java.lang.Thread#run()
    */
   public void run() {
      /*if (mcdir == null || !mcdir.exists()) {
         signalDone("Couldn't discover Minecraft home directory.");
         return;
      }
      File saves = new File(mcdir,"saves");
      if (!saves.exists()) {
         signalDone("Couldn't find Minecraft.jar");
         return;
      }
      File destination = new File(mcdir,"backup/saves");
      try {
         destination.mkdirs();
      } catch (Exception e) { }
      if (!destination.exists()) {
         signalDone("Couldn't create destination directory for backup.");
         return;
      }
      List in = Util.getFiles(new File(mcdir,"saves"));
      for (int i = 0; i < in.size(); i++) {
         File f = (File)in.get(i);
         if (f.isDirectory()) {
            File dir = new File(destination,f.getName());
            if (!dir.exists()) {
               dir.mkdirs();
            }
            Util.compressFiles(f,new File(dir,f.getName() + "." + System.currentTimeMillis() + ".zip"));
            //Util.compressFiles(f,new File(destination,f.getName() + "/" + f.getName() + "." + System.currentTimeMillis() + ".zip"));
         } else {
            Util.compressFiles(f, new File(destination,f.getName() + "." + System.currentTimeMillis() + ".zip"));
         }
      }
      signalDone(null);*/
      signalDone(backupWorlds(mcdir));
   }


}
